import _ from 'lodash';

export default {
  state: {
    cart: [],
  },
  mutations: {
    addFood(state, { food }) {
      state.cart.push(food);
    },
    removeFood(state, { index }) {
      state.cart.splice(index, 1);
    },
  },
  actions: {
    addCart({ commit }, { food }) {
      commit('addFood', { food });
    },
    removeCart({ commit, state }, { food }) {
      // eslint-disable-next-line no-underscore-dangle
      const index = _.findLastIndex(state.cart, (item) => item._id === food._id);
      commit('removeFood', { index });
    },

  },
  modules: {
  },
  getters: {
    total(state) {
      let sum = 0;
      state.cart.forEach((item) => {
        sum += item.price;
      });
      return sum;
    },
  },
};
